layui.config({
    base: "/layui/"
}).extend({
    index: 'lib/index'
}).use(['index', 'form', 'jquery', 'upload', 'table'], function () {
    var form = layui.form,
        table = layui.table,
        upload = layui.upload,
        admin = layui.admin;

    var apply = false;

    /**
     * 用户基本资料新增提交
     */
    form.on('submit(base_mch_form_ok)', function (data) {

        layer.confirm("确认提交基本资料吗，切记不要重复点击", {
            btn: ["确认", "取消"]
        }, function (index, layero) {
            $.ajax({
                url: "/baseInfo/edit",
                data: data.field,
                type: "post",
                success: function (result) {
                    if (result.code == 1) {
                        layer.msg("编辑成功", {
                            offset: '32%'
                            , icon: 1
                            , time: 0
                            , btn: '知道了'
                            , btnAlign: "c"
                            , shade: 0.4
                            , yes: function () {
                                layer.closeAll();
                            }
                        })
                    } else {
                        layer.msg(result.message, {
                            offset: '32%'
                            , icon: 2
                            , time: 3000
                        }, function () {
                            layer.closeAll();
                        })
                    }
                }
            });
        }, function (index) {

        });

        return false;
    });


    $(".face_file").each(function () {

        var userId = $("#userId").val();

        var base_mch_id = $("#base_mch_id").val();

        var file_btn = $(this).find(".face_file_btn");

        var face_file_img = $(this).find(".face_file_img");

        var fade_file_tip = $(this).find(".fade_file_tip");

        var face_file_input = $(this).find(".face_file_input");

        var keyName = face_file_input.attr("keyName");

        var img_value = face_file_input.val();

        if (img_value != null && img_value != "" && img_value != undefined) {
            var img_url = "/baseInfo/getFiel?fielId=" + img_value;
            face_file_img.attr('src', img_url); //图片链接（base64
        }

        var uploadInst = upload.render({
            elem: file_btn
            , url: '/baseInfo/fielUpload'
            , data: {userId: userId, id: base_mch_id, keyName: keyName}
            , before: function (obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function (index, file, result) {
                    face_file_img.attr('src', result); //图片链接（base64）
                });
            }
            , done: function (res) {
                if (res.code == 1) {
                    //如果上传成功
                    face_file_input.val(res.data.id);
                    return layer.msg('上传成功');
                } else {
                    //上传失败
                    face_file_img.attr('src', ""); //图片链接（base64）
                    return layer.msg('上传失败');
                }
            }
            , error: function () {
                //演示失败状态，并实现重传
                fade_file_tip.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                fade_file_tip.find('.demo-reload').on('click', function () {
                    uploadInst.upload();
                });
            }
        });
    })
})



